package com.hbx.exception.handler;

import com.auth0.jwt.exceptions.JWTDecodeException;
import com.hbx.auth.exception.AuthException;
import com.hbx.exception.RspException;
import com.hbx.utils.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*
 * @Author:hbx
 * 
 * */
@RestControllerAdvice
public class CommonExceptionHandler {
    /**
     * 异常处理器
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Result resourcePlatformHandler(Exception e){
        Result r = new Result();
        if (e instanceof RspException){
            RspException ex = (RspException) e;
            r.setCode(ex.getCode()).setMsg(ex.getMessage());
        } else if (e instanceof AuthException){
            AuthException ex = (AuthException) e;
            r.setCode(ex.getCode()).setMsg(ex.getMsg());
        } else if (e instanceof JWTDecodeException){
            r.setCode(401).setMsg("用户身份认证失败");
        } else{
            r.setCode(500).setMsg("系统错误");
        }
        return r;
    }
}
